#!/bin/sh
################################################################################
#
# Copyright (C) 2022 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
################################################################################
# File: cpuisolation04.sh
#
# Description: Isolate the CPU number switchover task
#
# Authors:     Ma Feng - mafeng.ma@huawei.com
#
# History:     Mar 15 2022 - init scripts
#
################################################################################
source tst_oh.sh

do_setup()
{
    for i in $(seq 0 39); do
        sh cpu_pro > /dev/null 2>&1 &
    done
    if [ $? -eq 0 ]; then
        tst_res TINFO "Creating 40 normal processes pass."
    else
        tst_res TINFO "Creating 40 normal processes failed!"
    fi
}

do_test()
{
    local ret=0
    local sec=0
    local set_max_cpus=0
    local cpu_ctl=/sys/devices/system/cpu/cpu0/core_ctl/
    
    cat ${cpu_ctl}global_state
    while true
    do
        if [ $sec -eq 21600 ]; then
            tst_res TINFO "Time is over!"
            break;
        fi
        let sec++

        let set_max_cpus++
        if [ set_max_cpus -gt 4 ]; then
            set_max_cpus=1
        fi

        echo $set_max_cpus > ${cpu_ctl}max_cpus
        read_max_cpus=$(cat ${cpu_ctl}max_cpus)
        if [ $read_max_cpus -eq $set_max_cpus ]; then
            tst_res TINFO "${read_max_cpus} cpus are used!"
        else
            ret=$(( $ret + 1 ))
        fi

	cat ${cpu_ctl}global_state | sed -n '2p;4p;15p;17p;28p;30p;41p;43p;'
        
        sleep 2
    done
    
    if [ $ret -eq 0 ]; then
		tst_res TPASS "Isolate the CPU number switchover task pass."
	else
		tst_res TFAIL "Isolate the CPU number switchover task failed!"
	fi   
}

do_clean()
{
    killall cpu_pro
}

do_setup
do_test
do_clean
tst_exit


